package test;

import common.Utils;
import org.checkerframework.checker.units.qual.K;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.devtools.v85.indexeddb.model.Key;
import org.openqa.selenium.interactions.Actions;

import java.text.SimpleDateFormat;

public class UserCenterPage extends Utils {
    private static String URL = "http://113.44.92.31:65533/index.html";

    public UserCenterPage() {
        super(URL);
    }

    /**
     * 测试修改昵称
     * @throws InterruptedException
     */
    public void checkNickName() throws InterruptedException {
         /*//最大化窗口
        driver.manage().window().maximize();*/
        //进入用户中心
        SimpleDateFormat nickDate = new SimpleDateFormat("HH-mm-ss");
        String nickName = nickDate.format(System.currentTimeMillis());
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();

        Thread.sleep(250);
        element = driver.findElement(By.cssSelector("#setting_input_nickname"));
        new Actions(driver).click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.DELETE).sendKeys(nickName).perform();
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        Thread.sleep(250);
        assert driver.findElement(By.cssSelector("#settings_nickname")).getText().equals(nickName);
    }

    /**
     * 测试修改邮箱
     * @throws InterruptedException
     */
    public void checkEmail() throws InterruptedException {
        /*//最大化窗口+进入用户中心
        driver.manage().window().maximize();
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();*/

        SimpleDateFormat emailDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String emailName = emailDate.format(System.currentTimeMillis());
        Thread.sleep(250);
        element = driver.findElement(By.cssSelector("#setting_input_email"));
        Thread.sleep(250);
        new Actions(driver).click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.DELETE).sendKeys(emailName + "@qq.com").perform();
        driver.findElement(By.cssSelector("#setting_submit_email")).click();
        //获取当前文本框的所有的文本信息
        new Actions(driver).click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
        String selectedText = (String) ((JavascriptExecutor)driver).executeScript("return window.getSelection().toString();");
        assert selectedText.equals(emailName + "@qq.com");
    }

    /**
     * 测试修改电话
     */
    public void checkPhoneNumber () throws InterruptedException {
        /*//最大化窗口+进入用户中心
        driver.manage().window().maximize();
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();*/

        SimpleDateFormat numberDate = new SimpleDateFormat("HHmmss");
        String phoneNumber = numberDate.format(System.currentTimeMillis());
        //修改电话
        JavascriptExecutor js = (JavascriptExecutor) driver;
        Thread.sleep(250);
        element = driver.findElement(By.cssSelector("#setting_input_phoneNum"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        new Actions(driver).doubleClick(element).sendKeys("136" + phoneNumber + "10").perform();
        driver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
        Thread.sleep(50);
        element = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String resp = element.getText().split("\n")[1];
        assert resp.equals("成功");
    }

    /**
     * 测试修改密码
     * @throws InterruptedException
     */
    public void checkPassword() throws InterruptedException {
        /*//最大化窗口+进入用户中心
        driver.manage().window().maximize();
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();
*/
        //原密码
        JavascriptExecutor js = (JavascriptExecutor) driver;
        Thread.sleep(100);
        element = driver.findElement(By.cssSelector("#settings_input_oldPassword"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        new Actions(driver).click(element).sendKeys("1111").perform();
        //新原密码
        Thread.sleep(100);
        element = driver.findElement(By.cssSelector("#settings_input_newPassword"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        new Actions(driver).click(element).sendKeys("1111").perform();
        //确认密码
        Thread.sleep(100);
        element = driver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        new Actions(driver).click(element).sendKeys("1111").perform();

        Thread.sleep(100);
        element = driver.findElement(By.xpath("//*[@id=\"settings_submit_password\"]"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        Thread.sleep(100);
        element.click();
        Thread.sleep(500);
        assert !driver.getCurrentUrl().equals(URL);

        //重新登录
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("Cyan");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        Thread.sleep(1000);
        String afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
        //当前在主页
    }

    public void checkTextarea() throws InterruptedException {
         //进入用户中心
        Thread.sleep(250);
        driver.findElement(By.cssSelector("#index_nav_nickname")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();

        SimpleDateFormat textareaDate = new SimpleDateFormat("HH:mm:ss");
        String textareaName = textareaDate.format(System.currentTimeMillis());

        JavascriptExecutor js = (JavascriptExecutor)driver;
        Thread.sleep(50);
        element = driver.findElement(By.cssSelector("#settings_textarea_remark"));
        js.executeScript("arguments[0].scrollIntoView(true);",element);
        new Actions(driver).click(element).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.DELETE).sendKeys("自动化测试个人简介" + textareaName).perform();
        driver.findElement(By.cssSelector("#settings_submit_remark")).click();

        Thread.sleep(100);
        element = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String message = element.getText().split("\n")[1];
        Thread.sleep(50);
        assert message.equals("成功");
    }
}